<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="true"
      :onRowSelectChange="selectRowChange"
      :onColumnsChange="columns => this.columns = columns"
      :isExportExcel2="true"
      :exportFileName="'备件盘点审核.xlsx'"
    >
      <template slot="controls">
        <el-button size="small" :disabled="auditSwitch" type="primary" icon="el-icon-edit" @click="auditFunc(1)">审核通过</el-button>
        <el-button size="small" :disabled="auditSwitch"  icon="el-icon-edit" @click="auditFunc(2)">审核驳回</el-button>
        <el-button size="small" type="primary" icon="el-icon-download" :loading="downloading" @click="detailExportClick">明细导出</el-button>
      </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'服务商查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>

    <Drawer
      :visible.sync="openPopup "
      :title="'盘点单明细'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <inventoryDetail :params="currentLine" @close="close" />
    </Drawer>

    <BaseDialog  :visible.sync="closeForm" :title="auditTitle" destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <auditPopup :value="selectRow" :type="auditType" @close="close"/>
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {findStockInventoryAudit,detailExport} from '@/api/partsmanage/inventory/stockInventoryAudit/index';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import inventoryDetail from '@/pages/partsmanage/inventory/stockInventoryAudit/customPopup/inventoryDetail';
import auditPopup from '@/pages/partsmanage/inventory/stockInventoryAudit/customPopup/auditPopup';
import { exportFile } from '@/utils/index';
export default {
  name: "partInventory",
  mixins: [dictionary],
  components: {ownerSeacher,inventoryDetail,auditPopup},
  data() {
    return{
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: findStockInventoryAudit,
      issearch: false,
      fetchParams: {xhrAbort: true},
      currentLine:null,
      openPopup:false,
      auditSwitch:true,
      selectRow:[],
      auditTitle:null,
      closeForm:false,
      downloading: false,
      auditType:null
    };
  },
  async created() {
  },
  methods: {
    /**
     * 盘点审核明细导出
     */
    detailExportClick(){
      this.downloading = true;
       detailExport(this.fetchParams).then(res => {
        exportFile(res.data, '盘点审核明细.xls');
        this.downloading = false;
      }).catch(res =>{
        this.downloading = false;
      })
    },
    auditFunc(type){
      if(type == 1){//审核通过
        this.auditTitle = "审核通过"
      }else if(type == 2){//审核驳回
        this.auditTitle = "审核驳回"
      }
      this.auditType = type;
      this.closeForm = true;
    },
    selectRowChange(data){
      let mark = 0;
      if(data.length == 0){
        mark = 1;
      }
      data.forEach(row => {
        if(row.auditStatus == null ||  row.auditStatus != 30441001){
          mark = 1;
        }
      });
      if(mark == 1){
        this.auditSwitch = true;
      }else{
        this.auditSwitch = false;
      }
      this.selectRow = data;

    },
    openDetail(row){
      this.currentLine = row;
      this.openPopup = true;
    },
    close(){
      this.openPopup = false;
      this.closeForm = false;
      // this.changeHandle();
    },
    createTopFilterList() {
      return[
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'requestNum',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '服务商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '服务商简称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'auditStatus',
          itemList: this.createDictList('3044'),
          placeholder: '请选择...'
        },
        {
          type: 'INPUT',
          label: '盘点单号',
          fieldName: 'inventoryNum',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          fieldName: 'requestStartTime|requestEndTime',
          label: '申请日期',
          initialValue: [],
          dateType: 'daterange'
        },
      ]
    },
    createTableColumns () {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          align:"center",
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          fixed: 'left',
          align:"center",
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.openDetail(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '申请单号',
          dataIndex: 'requestNum',
          sorter: true,
          align:"center"
        },
        {
          title: '服务商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          align:"center"
        },
        {
          title: '服务商简称',
          dataIndex: 'dealerName',
          sorter: true,
          align:"center"
        },
        {
          title: '审核状态',
          dataIndex: 'auditStatus',
          align:"center",
          dictItems:this.createDictList('3044'),
          sorter: true
        },
        {
          title: '审核意见',
          dataIndex: 'auditOpinion',
          sorter: true,
          align:"center",
          width: 180,
        },
        {
          title: '差异品种',
          dataIndex: 'differenceVariety',
          sorter: true,
          align:"center"
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          align:"center",
          width: 180,
        },
        {
          title: '盘点单号',
          dataIndex: 'inventoryNum',
          sorter: true,
          align:"center"
        },
        {
          title: '申请时间',
          dataIndex: 'requestTime',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true
        },
        {
          title: '审核时间',
          dataIndex: 'auditTime',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          sorter: true
        }
      ]
    },
    seacherFun(){
      this.issearch = true
    },
    dealercloseHandler(val){
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
        this.topFilterList.find(x=>x.fieldName=='dealerName').initialValue=val.dealerShortname;
      }
      this.issearch=false;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style lang="less" scoped>
</style>
